<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Random;
use think\Config;
use think\Request;
use think\Validate;

/**
 * @title 会员接口
 */
class User extends Api
{
    protected $noNeedLogin = ['login'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

    }



    /**
     * @title 会员登录
     * @description
     * @param name:account type:string require:1  other: default: desc:账号
     * @param name:password type:string require:1  other: default: desc:密码
     * @return res:结果
     * @author
     * @url /api/user/login
     * @method POST
     */
    public function login()
    {
        $account = $this->request->post('account');
        $password = $this->request->post('password');
        if (!$account || !$password) {
            $this->error(__('Invalid parameters'));
        }
        $ret = $this->auth->login($account, $password);
        if ($ret) {
            $data = ['userinfo' => $this->auth->getUserinfo()];
            $this->success(__('Logged in successful'), $data);
        } else {
            $this->error($this->auth->getError());
        }
    }

    /**
     * @title 获取用户信息
     * @description
     * @return res:结果
     * @author
     * @url /api/user/get_userinfo
     * @method POST
     */
    public function get_userinfo(Request $request){
        $this->success("", $this->auth->getUserinfo());
    }



    /**
     * @title 退出登录
     * @description
     * @return res:结果
     * @author
     * @url /api/user/logout
     * @method POST
     */
    public function logout()
    {
        if (!$this->request->isPost()) {
            $this->error(__('Invalid parameters'));
        }
        $this->auth->logout();
        $this->success(__('Logout successful'));
    }

    /**
     * @title 修改密码
     * @description
     * @param name:account type:string require:1  other: default: desc:账号
     * @param name:old_password type:string require:1  other: default: desc:旧密码
     * @param name:new_password type:string require:1  other: default: desc:新密码
     * @return res:结果
     * @author
     * @url /api/user/edit_password
     * @method POST
     */
    public function edit_password(Request $request){

        $old_password = $request->param("old_password");
        $new_password = $request->param("new_password");
        $ret = $this->auth->changepwd($new_password, $old_password);
        if ($ret) {
            $this->success("修改成功请重新登录");
        }
        $this->error("修改密码失败，请检查旧密码是否正确");
    }

    /**
     * @title 获取用户里列表
     * @description
     * @param name:rule type:string require:1  other: default: desc:身份1=负责人,2=检查员,3=安全总监,4=上级检查,不传全部,多种使用逗号链接，如1,3
     * @return res:结果
     * @author
     * @url /api/user/get_user_list
     * @method POST
     */
    public function get_user_list(Request $request){
        $rule = $request->param("rule");
        $user_list = \app\common\model\User::where(function ($query) use ($rule){
            if (!empty($rule)) {
                $query->where("rule", "in", $rule);
            }
        })->select();

        $this->success("", $user_list);
    }

}
